<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户" prop="u.id"
                :rules="[
                  {required: true, message:'用户不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择用户"
            labelName = 'nickname'
            labelValue = 'id'
            :value = "inputForm.u.id"
            :limit="1"
            @getValue='(value) => {inputForm.u.id=value}'
            :columns="[
            {
              prop: 'nickname',
              label: '昵称'
            }
            ]"
            :searchs="[
            {
              prop: 'nickname',
              label: '昵称'
            }
            ]"
            dataListUrl="/member/member/list"
            entityBeanName="member"
            queryEntityUrl="/member/member/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="商品名称" prop="title"
                :rules="[
                  {required: true, message:'商品名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写商品名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="商品缩略图" prop="icon"
                :rules="[
                  {required: true, message:'商品缩略图不能为空', trigger:'blur'}
                 ]">
              <el-upload ref="icon"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/memberorder/memberOrder`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.icon = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.icon = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="iconArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单价" prop="price"
                :rules="[
                  {required: true, message:'单价不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写单价"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数量" prop="quantity"
                :rules="[
                  {required: true, message:'数量不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.quantity" placeholder="请填写数量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="总价" prop="money"
                :rules="[
                  {required: true, message:'总价不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.money" placeholder="请填写总价"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="联系人" prop="linkman"
                :rules="[
                 ]">
              <el-input v-model="inputForm.linkman" placeholder="请填写联系人" disabled    ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="手机号" prop="mobile"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mobile" placeholder="请填写手机号"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户地址" prop="address"
                :rules="[
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写用户地址"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户地址详情" prop="addressDetail"
                :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.addressDetail" placeholder="请填写用户地址详情"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="快递单号" prop="expNo"
                :rules="[
                 ]">
              <el-input v-model="inputForm.expNo" placeholder="请填写快递单号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="订单状态" prop="orderState"
                :rules="[
                  {required: true, message:'订单状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.orderState" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('order_state')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="支付状态" prop="payState"
                :rules="[
                  {required: true, message:'支付状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.payState" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发货时间" prop="deliveryTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.deliveryTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="订单备注" prop="remark"
                :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remark" placeholder="请填写订单备注"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        iconArra: [],
        inputForm: {
          id: '',
          u: {
            id: ''
          },
          linkman: '',
          mobile: '',
          addressDetail: '',
          title: '',
          icon: '',
          price: '',
          quantity: '',
          money: '',
          address: '',
          expNo: '',
          orderState: '',
          payState: '',
          remark: '',
          deliveryTime: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建用户订单`
        } else if (method === 'edit') {
          this.title = '修改用户订单'
        } else if (method === 'view') {
          this.title = '查看用户订单'
        }
        this.iconArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/memberorder/memberOrder/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.memberOrder)
              console.log(data)
              this.inputForm.linkman  = data.linkman
              this.inputForm.mobile  = data.mobile
              this.inputForm.address  = data.address
              this.inputForm.addressDetail  = data.addressDetail
              this.inputForm.icon.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.iconArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/memberorder/memberOrder/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>